package com.example.prarranque5;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.androidplot.series.XYSeries;
import com.androidplot.xy.LineAndPointFormatter;
import com.androidplot.xy.SimpleXYSeries;
import com.androidplot.xy.XYPlot;

import java.text.DecimalFormat;
import java.util.Arrays;

/**
 * Created by JuanJoseSanz on 28/07/13.
 */
public class prDynPlot extends MainActivity implements View.OnClickListener{


    private int nPuntos , intcaca ;
    private double jIni , djIni ;
    private Number[] myArray , myArray2 , myArray3 ;
    private XYPlot dynamicPlot, staticPlot, mySimpleXYPlot;
    private LineAndPointFormatter series1Format ;
    private XYSeries series1 ;
    //    private MyPlotUpdater plotUpdater;
    private Button leftButton, rightButton ;
    private TextView textPrDynPlot ;

//    private class MyPlotUpdater {
//    }




    @Override
    public void onCreate(Bundle savedInstanceState) {

        // android boilerplate stuff
        super.onCreate(savedInstanceState);

        setContentView(R.layout.prdp); // initialize our XYPlot reference:


        leftButton = (Button) findViewById(R.id.leftButton) ;
        leftButton.setOnClickListener(this);

        rightButton = (Button) findViewById(R.id.rightButton) ;
        rightButton.setOnClickListener(this); ;

        textPrDynPlot = (TextView) findViewById(R.id.textPrDynPlot) ;

        dynamicPlot = (XYPlot) findViewById(R.id.dynamicPlot);

//---        plotUpdater = new MyPlotUpdater(dynamicPlot);
        // only display whole numbers in domain labels
//        dynamicPlot.getGraphWidget().setDomainValueFormat(new DecimalFormat("0"));

        // Create a couple arrays of y-values to plot:
        nPuntos = 100 ;
        myArray = new Number[nPuntos];
        myArray2 = new Number[nPuntos];
        myArray3 = new Number[nPuntos];

//---            try {
//---                Thread.sleep(1000);
//---            } catch (InterruptedException ex) {}

        jIni = 0d ;
        djIni = 0.5d ;

        textPrDynPlot.setText(""+white);

        series1Format = new LineAndPointFormatter(
                null,
//                Color.rgb(0, 0, 255),                   // line color
                red,
//                Color.rgb(255, 255, 255),               // point color
                null);                                  // fill color (none)

        dynamicPlot.addSeries(seriesBuilding1(nPuntos,jIni), series1Format);


//            Number[] series1Numbers = {Math.sin(0d), Math.sin(1d), Math.sin(2d), Math.sin(3d), Math.sin(4d), Math.sin(5d)};
//            Number[] series2Numbers = {1, 2, 4, 9, 16, 25};
//        LineAndPointFormatter f1 = new LineAndPointFormatter(Color.rgb(0, 0, 200), null,
//                Color.rgb(0, 0, 80));
            // Turn the above arrays into XYSeries':
            // Create a formatter to use for drawing a series using LineAndPointRenderer:
//        dynamicPlot.addSeries(series1,
//                new LineAndPointFormatter(Color.rgb(0, 0, 200), Color.rgb(0, 0, 100), null));
//        dynamicPlot.addSeries(myArray, f1);
//        dynamicPlot.addSeries(myArray, new LineAndPointFormatter(Color.rgb(0, 0, 0), null, Color.rgb(0, 80, 0)));
            // create a series using a formatter with some transparency applied:
//        LineAndPointFormatter f1 = new LineAndPointFormatter(Color.rgb(0, 0, 200), null, Color.rgb(0, 0, 80));
//        f1.getFillPaint().setAlpha(220);
//        dynamicPlot.addSeries(series2Numbers, f1);
//
//        dynamicPlot.setGridPadding(5, 0, 5, 0);
//---            dynamicPlot.redraw();



    }







    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }



//????????????????????????????????????????
//?????????????????????????????????????????  POR QUE "onClick" TIENE QUE SER PUBLIC?????
//????????????????????????????????????????
    @Override
    public void onClick(View v) {

        if(v.getId()==R.id.leftButton)
        {
//                intcaca = 42 ;
//                textPrDynPlot.setText("RESULTADO=" + intcaca);
          // get handles to our View defined in layout.xml:
                dynamicPlot.clear();
                jIni = jIni + djIni ;
                dynamicPlot.addSeries(seriesBuilding1(nPuntos,jIni), series1Format);
                dynamicPlot.redraw();
        }



        if(v.getId()==R.id.rightButton)
        {
            try {
                Thread.sleep(500);
            } catch (InterruptedException ex) {}

            dynamicPlot.clear();
            jIni = jIni - djIni ;
            dynamicPlot.addSeries(seriesBuilding1(nPuntos,jIni), series1Format);
            dynamicPlot.redraw();
        }


    }






    private XYSeries seriesBuilding1(int nP, double jI){
            Number[] array = new Number[nP];
            XYSeries series ;
            for (int j=0 ; j<=nP-1 ; j++)   {
                array[j] = Math.cos(0.1d* j + jI) ;
            }
            series = new SimpleXYSeries(
                    Arrays.asList(array),          // SimpleXYSeries takes a List so turn our array into a List
                    SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, // Y_VALS_ONLY means use the element index as the x value
                    " ");                             // fill color (none)
            return series ;
    }

}
